1
A Transaction Is One Unit of Work
DSAI2202 Transactions in SQL Databases
00:00

Data changes rarely arrive alone: a transfer must debit one account and credit another, and an order must reserve stock and record a payment. If the system stops halfway through, the database is left describing a world that never existed. A transaction is the database's answer to that problem — a sequence of one or more operations that the database treats as a single, indivisible unit of work.

All of it, or none of it

Inside a transaction, every operation belongs to one outcome. The database never exposes a finished state in which only part of the transaction happened. The classic illustration is a transfer between two bank accounts: reduce the balance of the source account, then increase the balance of the destination account. If only the first step succeeded, money would vanish; if only the second, money would appear from nowhere. Wrapping both steps in one transaction removes both possibilities.

This all-or-nothing guarantee is called atomicity. Commit is the act that makes the transaction's changes permanent; a transaction that never commits leaves no trace at all. Durability adds the final piece: once a commit has returned, the change survives even if the machine loses power a moment later.

A transaction is one unit of work A Transaction Is One Unit of Work Atomicity: all of it, or none of it. Transaction boundary one unit of work Step 1 — Debit the source account Step 2 — Credit the destination account Intermediate states exist inside the boundary, but they are private to this transaction. ✓ COMMIT Both changes permanent Survives a crash after commit ↺ ROLLBACK Neither account changed Leaves no trace at all A transaction is a sequence of one or more operations treated as a single, indivisible unit of work.

A transaction is a boundary

It helps to picture a transaction as a boundary drawn around a group of statements. Before the boundary and after it, the data satisfies the rules the structure imposes. Inside the boundary, intermediate states exist — one account is already debited while the other is not yet credited — but those states are private to the transaction. Later statements in other sessions do not treat them as the truth.

Two jobs at once

Transactions therefore do two jobs. The first is failure handling: if the process crashes, the connection drops, or a statement raises an error, the database can restore the state that existed before the transaction began. The second is visibility control: while the work is in progress, other readers are shielded from the unfinished result. These two jobs are related but not identical — atomicity decides whether changes are kept, while visibility is governed by isolation, the setting that controls how much concurrent transactions may see of each other.

Keep the unit on one connection
Transaction state belongs to a connection or session, so the statement that opens the transaction and the commit that ends it must run on the same connection. Many drivers also run in autocommit mode, where every single statement is its own transaction — a multi-statement unit needs autocommit switched off or an explicit start.